<?php
declare (strict_types = 1);

namespace app\admin\model\erp;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;

/**
 * 产品类别模型
 */
class ErpGoodsTypes extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'erp_goods_types';

    use ModelTrait;

    // 设置字段信息
    protected $schema = [
        'id'	        => 'int',
        'pid'           => 'int',
        'name'          => 'varchar',
        'code'          => 'varchar',
        'level'         => 'tinyint',
        'update_at'     => 'int'
    ];

    public static function addType($type,$item) {
        if(!$type || !$item) return [];

        $sub = ErpSubjects::where('rel_id', '=', $type['id'])->where('rel_type','=','type')->find();
        if(!$sub) return [];
        $level = $sub['level'] + 1;
        $code = ErpSubjects::where('code','like', "{$sub['code']}%")->where('level',$level)->max('code');
        if($code <= $sub['code']) $code = $sub['code'] . '000';
        $code = $code + 1;
        self::beginTrans();
        $bool = true;
        $data = [];
        $item['code'] = $code;
        $res = self::create($item);
        if($res) {
            $data = $res->toArray();
            $aitem = [
                'pid'       => $sub['id'],
                'rel_id'    => $data['id'],
                'rel_type'  => 'type',
                'code'      => $code,
                'name'      => $data['name'],
                'pname'     => $sub['pname'],
                'level'     => $level,
                'luck'      => 1,
                'edit_luck' => 1,
                'update_at' => time()
            ];
            $sb = ErpSubjects::create($aitem);
            if(!$sb) $bool = false;
        } else {
            $bool = false;
        }
        self::checkTrans($bool);
        return $bool ? $data : [];
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAll()
    {
        $typeAll = self::order('level ASC')->select()->toArray();
        return $typeAll;
    }
    /**
     * * @param int $op 0=列表、1=左侧树形、2=select
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function typeAll($op = 0)
    {
        $typeAll = self::order('level ASC')->select()->toArray();
        if($op == 2)
            return self::tidySelect($typeAll);
        else if($op == 1)
            return self::tidyLeftTree($typeAll);
        else
            return self::tidyTree($typeAll);
    }

    /**
     * 页面左侧树形展示
     * @param $list
     * @param int $pid
     * @param array $navList
     * @return array
     */
    public static function tidyLeftTree($list)
    {
        $data[] = [
            'title' => '物料分类',
            'spread' => true,
            'disabled' => false,
            'id' => '0',
        ];
        return self::leftTree($list, $pid = 0, $data);
    }
    public static function leftTree($list, $pid = 0, $navList = [])
    {
        foreach ($list as $k => $menu) {
            if ($menu['pid'] == $pid) {
                $info = [
                    'title' => $menu['name'],
                    'spread' => true,
                    'id' => $menu['code'],
                ];
                unset($list[$k]);
                $children = self::leftTree($list, $menu['id']);
                if($children) $info['children'] = $children;
                $navList[] = $info;
            }
        }
        return $navList;
    }
    /**
     * select选择项
     * @param $list
     * @param int $pid
     * @param array $navList
     * @return array
     */
    public static function tidySelect($list, $pid = 0, $navList = [])
    {
        foreach ($list as $k => $menu) {
            if ($menu['pid'] == $pid) {
                unset($list[$k]);
                $menu['children'] = self::tidySelect($list, $menu['id']);
                $navList[] = $menu;
            }
        }
        return $navList;
    }

    /**
     * 列表页面展示
     * @param $list
     * @param int $pid
     * @param array $navList
     * @return array
     */
    public static function tidyTree($list, $pid = 0, $navList = [])
    {
        foreach ($list as $k => $menu) {
            if ($menu['pid'] == $pid) {
                unset($list[$k]);
                $children = self::tidyTree($list, $menu['id']);
                if($children) {
                    $menu['nocheck'] = true;
                    $menu['children'] = $children;
                }
                $navList[] = $menu;
            }
        }
        return $navList;
    }
}
